mod proto;

use proto::hello::say_server::*;
use tonic::{Status, Response, Request, transport::Server, Code, Interceptor};
use crate::proto::hello::{HelloResponse, HelloRequest};

#[tokio::main]
async fn main()-> Result<(), Box<dyn std::error::Error>> {
    let addrs = vec!["[::1]:6661","[::1]:6662"];
    for addr in addrs.into_iter(){
        tokio::spawn(async move{
            let say = HelloServer{my_addr:addr};
            let say_server = SayServer::new(say);
            Server::builder()
                .add_service(say_server)
                .serve((addr).parse().unwrap()).await;
        });
    }
    loop {
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    }
    Ok(())
}

pub struct HelloServer{
    my_addr:&'static str,
}


#[tonic::async_trait]
impl Say for HelloServer{
    async fn say_hello(&self, request: Request<HelloRequest>) -> Result<Response<HelloResponse>, Status> {
        wd_log::log_info_ln!("server[{}] recover msg:{} ",self.my_addr,request.get_ref().value);
        let resp = Response::new(
            HelloResponse{value:format!("nihao i am {}",self.my_addr)}
        );
        Ok(resp)
    }
}

